package com.yb.sys.user.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yb.core.bean.Resp;
import com.yb.core.constant.HttpConstant;
import com.yb.sys.user.bean.SysHotel;
import com.yb.sys.user.mapper.SysHotelMapper;
import com.yb.sys.user.repository.SysHotelDao;
import com.yb.sys.user.service.ISysHotelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yb.sys.user.vo.PageReq;
import com.yb.sys.user.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚兵
 * @since 2022-11-23
 */
@Service
@Slf4j
public class SysHotelServiceImpl extends ServiceImpl<SysHotelMapper, SysHotel> implements ISysHotelService {

    @Autowired
    SysHotelMapper sysHotelMapper;

    @Autowired
    SysHotelDao sysHotelDao;

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public Resp init() {
        List<SysHotel> sysHotels = this.list();

        log.info("sysHotels:{}",sysHotels);
        try {
            sysHotelDao.saveAll(sysHotels);
            return Resp.ok().data(HttpConstant.SUCCESS);
        }catch (Exception e){
            return Resp.error().data(e);
        }
    }

//    @Override
//    public PageResult page(PageReq page) {
//        log.info("page=sortItems={},sort={}", JSONObject.toJSONString(page.getSorts()),page.getSort());
//        PageRequest pageRequest = PageRequest.of (page.getPage()-1, page.getSize(), page.getSort());
//        Page<SysHotel> resultPage = sysHotelDao.findAll(pageRequest);
//        return new PageResult().pageReq(page).resultPage(resultPage);
//    }
    @Override
    public PageResult page(PageReq page) {
        log.info("page=sortItems={},sort={}", JSONObject.toJSONString(page.getSorts()),page.getSort());
        PageRequest pageRequest = PageRequest.of (page.getPage()-1, page.getSize(), page.getSort());
        Page<SysHotel> resultPage = sysHotelDao.findAll(pageRequest);
        return new PageResult().pageReq(page).resultPage(resultPage);
    }
}
